$(function () {
    var ue = UE.getEditor("editor", {
        'serverUrl': '/ueditor/upload/',
        toolbars: [[
            'undo', //撤销
            'redo', //重做
            'bold', //加粗
            'italic', //斜体
            'source', //源代码
            'blockquote', //引用
            'selectall', //全选
            'insertcode', //代码语言
            'fontfamily', //字体
            'fontsize', //字号
            'simpleupload', //单图上传
            'emotion' //表情
        ]]
    });
    window.ue = ue;


    $('#publish-comment-btn').click(function () {
        var is_loginE = $('#login_flag');
        var is_login = is_loginE.attr('is_login');
        if (!is_login) {
            window.location = '/login/'
        }
        var author_id = is_loginE.attr('author_id');
        var content = window.ue.getContent();
        var thread_id = is_loginE.attr('thread_id');

        zlajax.post({
            'url': '/add_comment/',
            'data': {
                'content': content,
                'author_id': author_id,
                'thread_id': thread_id
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    xtalert.alertSuccessToast('评论成功');
                    setTimeout(function () {
                        window.location.reload()
                    }, 600)
                } else {
                    xtalert.alertInfo(data['message'])
                }
            },
            'fail': function () {
                xtalert.alertNetworkError()
            }
        })

    });

    $('.option').click(function (e) {
        $(this).css('background-color', 'rgba(185, 185, 185, 0.45)');
        var front_user = $('#login_flag').attr('is_login');
        if (front_user != '1') {
            window.location.href = '/login/';
            return
        }
        var type = $(this).attr('flag');
        var url = '';
        if (type == '1') {
            url = '/like/'
        } else {
            url = url = '/collect/'
        }
        var thread_id = $('.header').attr('thread_id');
        zlajax.get({
            'url': url + thread_id,
            'success': function (data) {
                if (data['code'] == 200) {
                    xtalert.alertSuccessToast('操作成功');
                } else {
                    xtalert.alertInfo(data['message'])
                }
            },
            'fail': function () {
                xtalert.alertNetworkError()
            }
        })
    });

    $('#delete-thread').click(function (e) {
        var thread_id = $('.header').attr('thread_id');
        xtalert.alertConfirm({
            title: '确定删除吗',
            confirmCallback: function () {
                zlajax.get({
                    'url': '/delitem/?iid=' + thread_id,
                    'success': function (data) {
                        if (data['code'] == 200) {
                            window.location.href = '/'
                        } else {
                            xtalert.alertInfo(data['message'])
                        }
                    },
                    'fail': function () {
                        xtalert.alertNetworkError()
                    }
                })
            }
        })
    });
    
    $('#edit-thread').click(function (e) {
        e.preventDefault();
        tid = $(this).attr('tid');
        window.location.href = '/thread?tid=' + tid
    });
});